import LinkedList from "../linked-list/LinkedList.js";

export default class Stack {
  constructor() {
    this.list = new LinkedList();
  }

  /**
   * 压栈
   * @param {*} value 需要入栈的数据
   * @returns {Stack}
   */
  push(value) {
    return this.list.insert(value, 0);
  }

  /**
   * 弹出栈顶数据
   * @returns {*}
   */
  pop() {
    const head = this.list.removeAt(0);

    return head ? head.value : null;
  }

  /**
   * 获取栈顶数据
   * @returns {*}
   */
  peek() {
    return this.list.head ? this.list.head.value : null;
  }

  /**
   * 获取栈的大小
   * @returns {number}
   */
  size() {
    return this.list.size();
  }

  /**
   * 获取栈是否为空
   * @returns {boolean}
   */
  isEmpty() {
    return this.list.size() === 0;
  }

  /**
   * 清空栈
   */
  clear() {
    this.list = new LinkedList();
  }

  /**
   * @param {function} [callback]
   * @returns {string}
   */
  toString(callback) {
    return this.list.toString(callback);
  }
}
